<?php
//后台商城类别控制器
namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;

class ShopController extends CommonController
{ 
    //查看购物车
	public function index()
    {
		/*$fname = $_GET['fname'];
		$fid = $_GET['fid'];
		//判断此小板块下是否有类别页
		$k = \DB::table("m_genre")->where("name", $fname)->where("fid",$fid)->first();
		if(empty($k)){
			//没有返回主页
			return redirect('home/index');
		}
		$p = \DB::table("m_pag")->get();
		$s = \DB::table("m_search")->where("fid",$fid)->get();
		$m = \DB::table("m_info")->where("fid",$fid)->get();
		return view("home.port",['forum'=>$f,'genre'=>$g,'minfo'=>$m, 'notice'=>$n,'notices'=>$z,'friendlink'=>$fl, 'search'=>$s,'type'=>$t,'pag'=>$p, 'port'=>$k]);*/
		
		$z = \DB::table("notice")->where('status','0')->get();
		$n = \DB::table("notice")->where('status','1')->get();
		$fl = \DB::table("friendlink")->get();
		$f = \DB::table("m_forum")->get();
		$t = \DB::table("m_type")->get();
		$g = \DB::table("m_genre")->get();
		return view("home.shop",['forum'=>$f,'genre'=>$g, 'notice'=>$n,'notices'=>$z,'friendlink'=>$fl, 'type'=>$t]);
    }
	
    //购物车添加商品
    public function add(Request $request,$id)
    {
		$z = \DB::table("notice")->where('status','0')->get();
		$n = \DB::table("notice")->where('status','1')->get();
		$fl = \DB::table("friendlink")->get();
		$f = \DB::table("m_forum")->get();
		$t = \DB::table("m_type")->get();
		$g = \DB::table("m_genre")->get();
		if(empty($request->all())){  
			$shop = \DB::table("m_info")->where("mid",$id)->select('name', 'brief', 'mid','activity')->first();
			$s = \DB::table("m_search")->where("mid",$id)->select('brand', 'price','color')->first();
			$shop->m = 1;
		}else{ 
			//获取要放入购物车中的商品
			$id = $request->all()['id'];
			//用户购买量
			$m = $request->all()['m'];
			$shop = \DB::table("m_info")->where("mid",$id)->select('name', 'brief', 'mid','activity')->first();
			$s = \DB::table("m_search")->where("mid",$id)->select('brand', 'price','color')->first();
			$shop->m = $m;
		}
		$p = \DB::table("m_pic")->where("mid",$id)->first();
		$shop->pic = $p->pic;
		$shop->brand = $s->brand;
		$shop->color = $s->color;
		$shop->price = $s->price;
        //先判断购物车中是否包含此商品
         if(session()->has("shoplist.{$id}")){
            //商品数量加1
            $ob = session()->get("shoplist.{$id}");
            //var_dump($ob);
            $ob->m += 1;
            session()->put("shoplist.{$id}",$ob);
        }else{
            //放入购物车中
            session()->put("shoplist.{$id}",$shop);
        }
        //session()->flush();
        return view("home.shop",['forum'=>$f,'genre'=>$g, 'notice'=>$n,'notices'=>$z,'friendlink'=>$fl, 'type'=>$t]);
    }
	
    //删除购物车中的某个商品
    public function del($id)
    {
        session()->forget('shoplist.'.$id);
        $z = \DB::table("notice")->where('status','0')->get();
		$n = \DB::table("notice")->where('status','1')->get();
		$fl = \DB::table("friendlink")->get();
		$f = \DB::table("m_forum")->get();
		$t = \DB::table("m_type")->get();
		$g = \DB::table("m_genre")->get();
		return view("home.shop",['forum'=>$f,'genre'=>$g, 'notice'=>$n,'notices'=>$z,'friendlink'=>$fl, 'type'=>$t]);
    }
    
    //清空购物车
    public function clear()
    {
       session()->put('shoplist',[]);
       $z = \DB::table("notice")->where('status','0')->get();
	   $n = \DB::table("notice")->where('status','1')->get();
	   $fl = \DB::table("friendlink")->get();
	   $f = \DB::table("m_forum")->get();
	   $t = \DB::table("m_type")->get();
	   $g = \DB::table("m_genre")->get();
	   return view("home.shop",['forum'=>$f,'genre'=>$g, 'notice'=>$n,'notices'=>$z,'friendlink'=>$fl, 'type'=>$t]);
    }
}
